﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Recycler : MonoBehaviour
{
    public float DestroyTime = 0;
    public bool DestroyImmediately = false;
    public void OnEnable()
    {
        if (DestroyImmediately)
        {
            Destroy(gameObject,0.2f);
        }
        if (DestroyTime > 0)
        {
            LateDestroy(DestroyTime);
        }
        if (GetComponent<ParticleSystem>() != null)
        {
            ParticleEffect();
        }
    }
    public void LateDestroy(float Time)
    {
        if (this.gameObject.activeInHierarchy)
        {
            StartCoroutine(LateDestroyCor(Time));
        }
    }
    IEnumerator LateDestroyCor(float Time)
    {
        yield return new WaitForSeconds(Time);
        if (this.gameObject.activeInHierarchy)
        {
            ObjectPool.Recycle(this.gameObject);
        }
    }
    public void ParticleEffect()
    {
        GetComponent<ParticleSystem>().Stop();
        GetComponent<ParticleSystem>().Play();
    }
    public void AnimationEffect()
    {
        GetComponentInChildren<Animation>().Stop();
        GetComponentInChildren<Animation>().Play();
    }
}
